package com.donn.securitycontrolplatform.dangerousgood.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.donn.securitycontrolplatform.common.Result;
import com.donn.securitycontrolplatform.dangerousgood.entity.TEquipmentStorage;
import com.donn.securitycontrolplatform.dangerousgood.response.Storage;
import com.donn.securitycontrolplatform.dangerousgood.response.StorageResponse;
import com.donn.securitycontrolplatform.dangerousgood.service.ITEquipmentStorageService;
import com.donn.securitycontrolplatform.db1.mapper.TEquipmentStorageMapper;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhenglx
 * @since 2022-07-08
 */
@Service
public class TEquipmentStorageServiceImpl extends ServiceImpl<TEquipmentStorageMapper, TEquipmentStorage> implements ITEquipmentStorageService {

    @Override
    public Result selectEquipmentStorageList(Page page) {
        try {
            QueryWrapper<TEquipmentStorage> queryWrapper = new QueryWrapper<>();
            return Result.success(page(page, queryWrapper));
        } catch (Exception e) {
            log.error("selectStorageList error :", e);
        }
        return Result.init(500, "fail", "");
    }

    @Autowired
    TEquipmentStorageMapper mapper;

    @Override
    public Result getLastWeekStorageInfo() {
        List<StorageResponse> responses = mapper.getLastWeekStorage(LocalDateTime.now().plusDays(-6), LocalDateTime.now());
        Map<Integer, Integer> lastWeekMap = new HashedMap();
        lastWeekMap.put(DayOfWeek.MONDAY.getValue(), 0);
        lastWeekMap.put(DayOfWeek.TUESDAY.getValue(), 0);
        lastWeekMap.put(DayOfWeek.WEDNESDAY.getValue(), 0);
        lastWeekMap.put(DayOfWeek.THURSDAY.getValue(), 0);
        lastWeekMap.put(DayOfWeek.FRIDAY.getValue(), 0);
        lastWeekMap.put(DayOfWeek.SATURDAY.getValue(), 0);
        lastWeekMap.put(DayOfWeek.SUNDAY.getValue(), 0);
        List<Integer> dateList = responses.stream().map(StorageResponse::getDate).collect(Collectors.toList());
        for (Map.Entry<Integer, Integer> map : lastWeekMap.entrySet()) {
            if (!dateList.contains(map.getKey())) {
                responses.add(new StorageResponse(map.getKey(), 0, 0, 0));
            }
        }
        responses.sort(Comparator.comparing(StorageResponse::getDate));
        List<Integer> storageInQuantityList = responses.stream().map(StorageResponse::getStorageInQuantity).collect(Collectors.toList());
        List<Integer> storageOutQuantityList = responses.stream().map(StorageResponse::getStorageOutQuantity).collect(Collectors.toList());
        List<Integer> storageQuantityList = responses.stream().map(StorageResponse::getQuantity).collect(Collectors.toList());
        Storage storage = new Storage();
        storage.setStorageInQuantity(storageInQuantityList);
        storage.setStorageOutQuantity(storageOutQuantityList);
        storage.setStorageQuantity(storageQuantityList);
        return Result.success(storage);
    }
}
